﻿#include "OperPhysicsPropRibbed.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "GUIDialog/GUIPhyscisDialog/PhysicsDialogRibbed.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKPhyscisPropertyRibbed.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsPhysicsProp.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKPhyscisPropertyRibInfo.h"

#include "updateBrowserTable.h"
#include "GUIWidget/AttributeSetDialog.h"

namespace ModelOper
{
    bool OperPhysicsPropRibbed::execGUI()
    {
        //按钮点击事件触发
        if (_emitter == nullptr) return false;
        const QString name = _emitter->objectName();
        if (name == "actionRibbed")
            createPhysicsProp();
        return false;
    }

    bool OperPhysicsPropRibbed::execProfession()
    {
        return false;
    }

    void OperPhysicsPropRibbed::createPhysicsProp()
    {
        //弹出对话框
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        GUI::PhysicsDialogRibbed dlg(GUI::PhysicsDlgExecType::Create, -1, mw);
        //更新列表
        if (QDialog::Accepted == dlg.exec())
            ModelOper::updateBrowserTable();
    }

    void OperPhysicsPropRibbed::duplicatePhysicsProp(const int propID)
    {
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        //编辑对话框
        GUI::PhysicsDialogRibbed dlg(GUI::PhysicsDlgExecType::Duplicate, propID, mw);
        //更新列表
        if (QDialog::Accepted == dlg.exec())
            ModelOper::updateBrowserTable();
    }

    void OperPhysicsPropRibbed::editPhysicsProp(const int propID)
    {
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        //编辑对话框
        GUI::PhysicsDialogRibbed dlg(GUI::PhysicsDlgExecType::Edit, propID, mw);
        //更新列表
        if (QDialog::Accepted == dlg.exec())
            ModelOper::updateBrowserTable();
    }

    void OperPhysicsPropRibbed::removePhysicsProp(const int propID)
    {
        //获取管理器
        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return;
        Acoustics::FITKAcousticsPhysicsPropManager* propMgr = phys->getPhysicsPropManager();
        if (propMgr == nullptr) return;
        //移除并更新列表
        propMgr->removeDataByID(propID);
        ModelOper::updateBrowserTable();
    }

    void OperPhysicsPropRibbed::setAttribute(const QString & name, const QString & text, const int attrIndex, const QList<int>& propID)
    {
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return;
        Acoustics::FITKAcousticsPhysicsPropManager* propMgr = phys->getPhysicsPropManager();
        if (propMgr == nullptr) return;
        //设置对话框
        int type = 1;
        if (attrIndex == 1)type = 2;
        GUI::AttributeSetDialog dlg(type, mw);
        dlg.setTextLabel(name);
        if (attrIndex == 1) {
            QList<Acoustics::FITKAcousticsAbsPhysicsProp*>propList = propMgr->getPhysicsProps(Acoustics::FITKAcousticsAbsPhysicsProp::APPUniformPlat);
            propList+= propMgr->getPhysicsProps(Acoustics::FITKAcousticsAbsPhysicsProp::APPGeneralLaminatePhysicalProperty);
            QList<QString> list;
            for (auto value : propList) {
                list += value->getDataObjectName();
            }
            dlg.setDefaultValueQString(list);
        }
        else{
            dlg.setDefaultValueDouble(text.toDouble());
        }
        if (dlg.exec() != QDialog::Accepted) return;


        // 基板
        QString phy = dlg.getValueQString();
        int phyID = -1;
        if (attrIndex == 1) {
            if(propMgr->getDataByName(phy))
                phyID = propMgr->getDataByName(phy)->getDataObjectID();
        }
        //筋属性
        const double value = dlg.getValueDouble();

        for (int id : propID)
        {
            Acoustics::FITKPhyscisPropertyRibbed* prop = dynamic_cast
                <Acoustics::FITKPhyscisPropertyRibbed*>(propMgr->getDataByID(id));
            if (prop == nullptr) continue;
            auto r1 = prop->getDri1RibInfo();
            auto r2 = prop->getDri2RibInfo();
            //不同列设置不同参数
            switch (attrIndex)
            {
            case 1: //基板
                prop->setSkinPropID(phyID); break;
            case 3: //第一条筋的加筋间距
                r1->setSpacing(value); break;
            case 4: //第一条筋的标准差
                r1->setSpacingDevFraction(value); break;
            case 7: //第二条筋的加筋间距
                r2->setSpacing(value); break;
            case 8: //第二条筋的标准差
                r2->setSpacingDevFraction(value); break;
            default: break;
            }
        }
        ModelOper::updateBrowserTable();
    }


}
